home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4620 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  106 lines

  1. Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
  2. From: ain.johnston@ubs.com (Johnston Ian (by ubsswop))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: So I have this pointer-to-member function...
  5. Date: 31 Jan 1996 10:39:33 GMT
  6. Organization: UBS
  7. Distribution: world
  8. Message-ID: <4engt5$plq@ubszh.fh.zh.ubs.com>
  9. References: <d4c1.smail.smayo@tiac.net>
  10. NNTP-Posting-Host: nol2179.fh.zh.ubs.com
  11.  
  12. In article <d4c1.smail.smayo@tiac.net>, smayo@tiac.net (Scott Mayo) writes:
  13. |> I'm trying to do something in C++ which instinct tells me shouldn't be hard to
  14. |> arrange, but the compiler isn't cooperating.
  15. |> 
  16. |> I have a class, call it C, whose purpose in life is to acquire an input
  17. |> stream, parse it into tokens, and then have the tokens tell it which member
  18. |> functions to call. For token "A" call member function A, and so on. An array
  19. |> of struct {char *token; void (C::*f)();} fits the bill well enough to manage
  20. |> that.
  21. |> 
  22. |> Now the tricky part. I want to derive class C1 and C2 from C. And I want C1
  23. |> (and C2, etc) to have their own such table for mapping tokens to member
  24. |> functions; in the absence of a match, I want to go up and search C's table and
  25. |> use what I find there.
  26. |> 
  27. |> "Ah," I innocently thought, "I'll just make each array of struct a static
  28. |> variable inside the class definition it goes with, and then it will be easy to
  29. |> automatically search the right table at the right time."
  30. |> 
  31. |> The compiler will have none of it. It takes one glance at the static struct
  32. |> {char*name; void (C::*f)();} list[] = {"A", C::A}; and has a hissy fit at the
  33. |> attempt to assign initializers. It's fine outside the class definition; it
  34. |> won't work inside.
  35. |> 
  36. |> I can clearly just put the arrays outside the class definitions, and just
  37. |> teach the class "search" function to know which array to go after, but it
  38. |> seems crass. That array is logically a part of the class behaviour, and if I
  39. |> end up creating a lot of these subclasses, I don't want to rely on
  40. |> remembering to tell each search function which magic array to use.
  41. |> 
  42. |> Suggestions? Am I missing something simple and useful here? Thanks. Mail
  43. |> appreciated, posting might be missed.
  44. |> 
  45.  
  46.  
  47. How about this:
  48.  
  49.  
  50.  
  51. struct C
  52. {
  53.     typedef void (C::*TokFunc)();
  54.  
  55.     struct Table
  56.     {
  57.     char *name;
  58.     TokFunc func;
  59.     };
  60.  
  61.     void tokIf();
  62.     void tokWhile();
  63.  
  64.     static Table c_lookup[];
  65. };
  66.  
  67.  
  68. C::Table C::c_lookup[] =
  69. {
  70.     { "if", &C::tokIf },
  71.     { "while", &C::tokWhile }
  72. };
  73.  
  74.  
  75. struct C1 : public C
  76. {
  77.     typedef void (C1::*TokFunc)();
  78.  
  79.     struct Table
  80.     {
  81.     char *name;
  82.     TokFunc func;
  83.     };
  84.  
  85.     void tokCase();
  86.     void tokDefault();
  87.  
  88.     static Table c1_lookup[];
  89. };
  90.  
  91. C1::Table C1::c1_lookup[] =
  92. {
  93.     { "case", &C1::tokCase },
  94.     { "default", &C1::tokDefault }
  95. };
  96.  
  97.  
  98.  
  99. Now each class's search function uses its own local table, and calls its
  100. base class's search function if the token is not found.
  101.  
  102. The search functions should actually call the token functions when they
  103. find a match.
  104.  
  105. Ian
  106.